﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LiteQueryWPF.Controls
{
    /// <summary>
    /// QueryPanel.xaml 的交互逻辑
    /// </summary>
    public partial class QueryPanel : UserControl
    {
        Entities.Query query;
        public Entities.Query Query
        {
            get { return query; }
            set { query = value; }
        }

        public int Rows
        {
            get
            {
                return plQuery.RowDefinitions.Count;
            }
            private set
            {
                if (value > 0)
                {
                    for (int i = 0; i < value; i++)
                    {
                        plQuery.RowDefinitions.Add(new RowDefinition());
                    }
                    foreach (RowDefinition item in plQuery.RowDefinitions)
                    {
                        item.Height = new GridLength(34);
                    }
                }
            }
        }

        public int Columns
        {
            get
            {
                return plQuery.ColumnDefinitions.Count;
            }
            private set
            {
                for (int i = 0; i < value; i++)
                {
                    plQuery.ColumnDefinitions.Add(new ColumnDefinition() { });
                }

                foreach (ColumnDefinition item in plQuery.ColumnDefinitions)
                {
                    item.Width = new GridLength(1, GridUnitType.Star);
                }
            }
        }

        public QueryPanel()
        {
            InitializeComponent();
        }

        public QueryPanel(Entities.Query q)
        {
            InitializeComponent();
            this.Query = q;
            this.Columns = query.Columns;
            this.Rows = query.Rows;
            InitFields();
            ;
        }

        void InitFields()
        {
            int row = 0;
            int col = 0;
            foreach (Entities.Field item in this.Query.Fields.OrderBy(x => x.Index))
            {
                UserControl field=null;
                if (item.Type == "compare")
                {
                    field = new FieldComparePanel(item)
                    {
                    };
                    plQuery.Children.Add(field);
                }
                else if (item.Type == "normal")
                {
                    field = new FieldPanel(item)
                    {
                    };
                    plQuery.Children.Add(field);
                }
                else if (item.Type == "date")
                {
                    field = new FieldDateComparePanel(item)
                    {
                    };
                    plQuery.Children.Add(field);
                }

                Grid.SetRow(field, row);
                Grid.SetColumn(field, col);

                if (col == Columns-1)
                {
                    col = 0;
                    row = row + 1;
                }
                else
                {
                    col += 1;
                }
            }
        }
    }
}
